const fs = require("fs");
const XLSX = require("xlsx");
const moment = require("moment");
const iconv = require("iconv-lite");

// 读取static目录下的所有文件
const files = fs.readdirSync("./static");
// console.log(files,'files')
// 遍历所有文件
files.forEach((file) => {
  // 如果文件不是以.txt结尾，跳过
  if (!file.endsWith(".txt")) return;

  // 读取日志文件
  const log = iconv.decode(fs.readFileSync(`./static/${file}`), "gbk");

  // 使用正则表达式提取信息
  const pattern =
    /(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) \d+ http get result form camera plate number is (\S+)/g;
  let matches;
  const data = [];
  while ((matches = pattern.exec(log))) {
    // console.log(matches[1],'matches')
    data.push([moment(matches[1]).format("YYYY-MM-DD HH:mm:ss"), matches[2]]);
  }

  // 创建Excel工作簿
  const wb = XLSX.utils.book_new();
  console.log(data,'data')
  const ws = XLSX.utils.aoa_to_sheet([["时间", "车牌号"], ...data]);
  XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
  // 将工作簿写入文件
  XLSX.writeFile(wb, `${file.split(".")[0].split("_")[2]}.xlsx`);
});



